'use strict'

const loaclVideo = document.getElementById('local_video');
const startButton = document.getElementById('start');
const closeButton = document.getElementById('close');

startButton.addEventListener('click', start);
closeButton.addEventListener('click', close);

closeButton.disabled = true;

var localStream;

function start() {
    navigator.mediaDevices.getUserMedia({ audio: true, video: true })
        .then(function(mediaStream) {
            console.log(mediaStream)
            startButton.disabled = true;
            closeButton.disabled = false;
            localStream = mediaStream;
            let s = new MediaStream();
            loaclVideo.srcObject = s;
            onactive: null
            s.onaddtrack = (res) => {
                console.log(res)
            }
            s.oninactive = (res) => {//不活跃的; 不活动的; 怠惰的; 闲置的
                console.log(res)
            }
            s.onremovetrack = (res) => {
                console.log(res)
            }

            for (const track of mediaStream.getTracks()) {
                s.addTrack(track, mediaStream);
            }
            // for (const track of s.getTracks()) {
            //     console.log(track);
            // }
            setTimeout(() => {

                s.removeTrack(s.getVideoTracks()[0]);
                /*for (const track of s.getTracks()) {
                    console.log(track);
                }*/
            }, 10000)

        }).catch(function(error) {
            console.log(JSON.stringify(error));
        });
}

function close() {
    console.log(localStream)
    startButton.disabled = false;
    closeButton.disabled = true;
    localStream.getTracks().forEach(track => track.stop());
}